package com.sinvo.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinvo.oa.entity.model.system.SysLoginLog;
import com.sinvo.oa.entity.vo.system.SysLoginLogQueryVo;
import com.sinvo.oa.mapper.SysLoginLogMapper;
import com.sinvo.oa.service.SysLoginLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description SysLoginLogService 实现类
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    @Override
    public void recordLoginLog(SysLoginLog sysLoginLog) {
        this.save(sysLoginLog);
    }

    @Override
    public Page<SysLoginLog> findLoginLogByPage(Page<SysLoginLog> page, SysLoginLogQueryVo sysLoginLogQueryVo) {
        //条件值
        String username = sysLoginLogQueryVo.getUsername();
        String createTimeBegin = sysLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginLogQueryVo.getCreateTimeEnd();

        //封装条件，判断roleName是否为空
        LambdaQueryWrapper<SysLoginLog> wrapper = new LambdaQueryWrapper<>();
        //条件不为空
        if (!StringUtils.isEmpty(username)) {
            //封装 like模糊查询
            wrapper.like(SysLoginLog::getUsername, username);
        }
        //时间范围
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge(SysLoginLog::getCreateTime, createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.lt(SysLoginLog::getCreateTime, createTimeEnd);
        }
        wrapper.orderByDesc(SysLoginLog::getCreateTime);
        // 调用方法实现
        Page<SysLoginLog> pageModel = this.page(page, wrapper);

        return pageModel;
    }
}
